package com.splant.android.smart.ProtocolLevel;

import com.splant.android.smart.SystemDefinedLevel.Utils.System_DataType_Util;

/**
 * Created by Administrator on 2017/4/13.
 */

public class sPlantCProtocol {

    public static final int UNFIND_USER = 10000;
    public static final int DEVICE_OFFLINE = 10001;
    public static final int DEVICE_BUSY = 10002;

    public static final byte HEAD_HEX = (byte) 0xBB;

    // private static byte[] userIdBytes={0x00,0x00,0x00,0x00};

    private static byte[] deviceIdBytes={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

    public sPlantCProtocol() {

    }

    private static sPlantCProtocol protocol;

    public static sPlantCProtocol getInstance() {
        synchronized (sPlantCProtocol.class) {
            if (protocol == null)
                protocol = new sPlantCProtocol();
        }
        return protocol;
    }

    private byte[] changelessBytes = {HEAD_HEX, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    public void setChangelessBytes(int userId, String deviceId) {
        byte[] bytes = System_DataType_Util.intToBytesSmallEndian(userId);
        for (int i = 0; i < bytes.length; i++) {
            changelessBytes[i + 1] = bytes[i];
        }
        if (deviceId.length() == 32) {

            byte[] dBytes = System_DataType_Util.hexStringToBytes(deviceId);
            for (int i = 0; i < dBytes.length; i++)
            {
                deviceIdBytes[i]=dBytes[i];
                changelessBytes[i + 5] = dBytes[i];
            }


        }
    }

    public byte[] getGrossParas() {

        byte[] gParasBytes = new byte[43];
        for (int i = 0; i < changelessBytes.length; i++)
            gParasBytes[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[20];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x24;

        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x00;

        byte[] deviceOrder_crc= CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            gParasBytes[21+i]=deviceOrder_crc[i];

        return gParasBytes;
    }

    public byte[] getSoilParas()
    {
        byte[] soilParasBytes = new byte[43];

        for (int i = 0; i < changelessBytes.length; i++)
            soilParasBytes[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[20];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x0C;

        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x00;

        byte[] deviceOrder_crc= CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            soilParasBytes[21+i]=deviceOrder_crc[i];

        return soilParasBytes;
    }
    public byte[] getAmbParas()
    {
        byte[] ambParasBytes = new byte[43];

        for (int i = 0; i < changelessBytes.length; i++)
            ambParasBytes[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[20];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x0A;

        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x00;

        byte[] deviceOrder_crc= CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            ambParasBytes[21+i]=deviceOrder_crc[i];

        return ambParasBytes;
    }

    public byte[] setWaterState(int state) {

        byte[] waterStateBytes = new byte[59];
        for (int i = 0; i < changelessBytes.length; i++)
            waterStateBytes[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[36];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x03;
        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x10;
        deviceOrder[20] = (byte) state;
        for (int i = 0; i < 15; i++)
            deviceOrder[21 + i] = 0x00;

        byte[] deviceOrder_crc=CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            waterStateBytes[21+i]=deviceOrder_crc[i];

        return waterStateBytes;
    }

    public byte[] setSpray(int state)
    {
        byte[] sprayBytes = new byte[59];
        for (int i = 0; i < changelessBytes.length; i++)
            sprayBytes[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[36];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x02;
        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x10;
        deviceOrder[20] = (byte) state;

        for (int i = 0; i < 15; i++)
            deviceOrder[21 + i] = 0x00;

        byte[] deviceOrder_crc=CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            sprayBytes[21+i]=deviceOrder_crc[i];

        return sprayBytes;
    }

    public byte[] setWorkMode(int mode)
    {
        byte[] workModeBytes = new byte[44];

        for (int i = 0; i < changelessBytes.length; i++)
            workModeBytes[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[21];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x09;
        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x01;
        deviceOrder[20] = (byte) mode;

        byte[] deviceOrder_crc=CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            workModeBytes[21+i]=deviceOrder_crc[i];

        return workModeBytes;
    }

    public byte[] setPoleDownUp(int state)
    {

        byte[] poleDownUpBytes = new byte[44];
        for (int i = 0; i < changelessBytes.length; i++)
            poleDownUpBytes[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[21];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x1D;
        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x01;
        deviceOrder[20] = (byte) state;

        byte[] deviceOrder_crc=CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            poleDownUpBytes[21+i]=deviceOrder_crc[i];

        return  poleDownUpBytes;
    }

    public byte[] setLightValue(int value)
    {
        byte[] lightValue = new byte[51];
        for (int i = 0; i < changelessBytes.length; i++)
            lightValue[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[28];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x01;
        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x08;
        deviceOrder[20] = (byte) value;

        for (int i = 0; i < 7; i++)
            deviceOrder[21 + i] = 0x00;

        byte[] deviceOrder_crc=CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            lightValue[21+i]=deviceOrder_crc[i];

        return lightValue;
    }

    public byte[] setLightTime(int startTime, int endTime) {

        byte[] lightTimeBytes = new byte[49];
        byte st = (byte) startTime;
        byte et = (byte) endTime;

        for (int i = 0; i < changelessBytes.length; i++)
            lightTimeBytes[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[26];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x27;
        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x06;
        deviceOrder[20] =st;
        deviceOrder[21] =0x00;
        deviceOrder[22] =0x00;
        deviceOrder[23] =et;
        deviceOrder[24] =0x00;
        deviceOrder[25] =0x00;

        byte[] deviceOrder_crc=CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            lightTimeBytes[21+i]=deviceOrder_crc[i];

        return lightTimeBytes;
    }


    public byte[] setLightMode(int mode)
    {

        byte[] lightModeBytes = new byte[44];
        for (int i = 0; i < changelessBytes.length; i++)
            lightModeBytes[i] = changelessBytes[i];

        byte[] deviceOrder=new byte[21];
        deviceOrder[0]=0x55;

        for (int i=0;i<deviceIdBytes.length;i++)
            deviceOrder[i+1]=deviceIdBytes[i];

        deviceOrder[17] = 0x29;
        deviceOrder[18] = 0x00;
        deviceOrder[19] = 0x01;
        deviceOrder[20] = (byte) mode;

        byte[] deviceOrder_crc=CRC16.addCrc16Bytes(deviceOrder);

        for (int i=0;i<deviceOrder_crc.length;i++)
            lightModeBytes[21+i]=deviceOrder_crc[i];

        return  lightModeBytes;
    }



    public int getSoilmoisture(byte[] datas,int postion)
    {
        int r=0;
        if (postion==0)
        {
            for (int i=0;i<4;i++)
            {
                int d=datas[23+3*i];
                if (d>0)
                    r=d;
            }
        }
        else
        {
            int p=postion-1;
            r=datas[23+3*p];
        }

        return r;
    }

    public int getSoilnutrient(byte[] datas,int postion)
    {
        int r=0;
        if (postion==0)
        {
            for (int i=0;i<4;i++)
            {
                int d=datas[24+3*i];
                if (d>0)
                    r=d;
            }
        }
        else
        {
            int p=postion-1;
            r=datas[24+3*p];
        }

        return r;
    }
}
